//  File:         ToDoList.cpp
//  Author:       Scott Nordmann & Lauren Wallace
//  Description:  Non-inline member function definitions.
#include "ToDoList.h"
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

ToDoList::ToDoList()
{
	
}
ToDoList::ToDoList(ifstream& ifsave)
{
	int numTasks = 0;
	double ctr =0;
	Task* tempTask;
	string tempCreator; 
	string tempTitl;
	string tempCont;
	
	ifsave >> numTasks;
	ifsave.ignore();
	while (ctr != (double)numTasks)
	{
		++ctr;
		getline(ifsave, tempCreator);
		getline(ifsave, tempTitl);
		getline(ifsave, tempCont);
		tempTask = new Task(tempCreator, tempTitl, tempCont, ctr);
		tasks.push_back(tempTask);
	}
}
void ToDoList::shutDown(ofstream& ofsave)
{
	ofsave << tasks.size() << endl;
	
	if (tasks.size() != 0)
	{
		vector<Task*>::iterator iter;
		for(iter=tasks.begin();iter !=tasks.end(); ++iter)
		{
			ofsave << (*iter)->getCreatorName() <<endl;
			ofsave << (*iter)->getTitle() <<endl;
			ofsave << (*iter)->getContent() <<endl;
		}
	}
}

void ToDoList::createTask(const string& creator)
{
	Task* temp = new Task(creator);

	temp->populate();

	tasks.push_back(temp);

	orderList();
}

void ToDoList::createTask(Task* tas)
{
	tasks.push_back(tas);
	orderList();
}
Task* ToDoList::createMail(const string& creator)
{
	Task* temp = new Task(creator);
	string target;

	temp->populate();

	cout << "Enter Recipient name: ";
	getline(cin, target);
	temp->setSendTo(target);

	return temp;
}
void ToDoList::display()
{
	int ctr=0;
	int choice=0;
	vector<Task*>::iterator iter;

	system("cls");
	
	cout << "Your ToDoList" << endl << endl;
	//
	//display the to do list
	//

	if ( ! ( tasks.empty() ) )
	{
		for(iter = tasks.begin(), ctr=0; iter != tasks.end(); ++iter)
		{	
			++ctr;
			cout << "[" << ctr << "] ";
			(*iter)->displayTitle();
			cout <<endl;
		}

		cout << "Which note would you like to view (enter number 1 - " << ctr << ")? ";
		cin >> choice;
		// range checked here- if it's out of bounds put choice in bounds
		if (choice < 1)
			choice = 1;
		if (choice > ctr)
			choice = ctr;
				
		--choice;//decriment choice so that it works with vector
				
		system("cls");
		
		//(tasks.at(choice))->display();
		viewTask(  *(tasks.at(choice))  );
	}
	else
		cout << "there are no Tasks" << endl;
}
void ToDoList::orderList()
{
	double ctr;
	string stop;
	Task* temp;
	vector<Task*>::iterator iter;
	vector<Task*>::iterator next;

	if (tasks.size()>1)
	{
		iter = (tasks.end()-1);
		next = (tasks.end()-2);
		while (iter != tasks.begin())
		{
			if (**iter < **next)
			{
				temp = *iter;
				*iter = *next;
				*next = temp;
				
			}
			--iter;
			if (next != tasks.begin()) 
				--next;
		}
		iter = (tasks.begin());
		next = (tasks.begin());
		++next;
		while (iter != tasks.begin())
		{
			if (**iter > **next)
			{
				temp = *iter;
				*iter = *next;
				*next = temp;
				++iter;
				if (next != tasks.begin()) 
					++next;
			}
		}
	}

	for(iter = tasks.begin(), ctr=0; iter != tasks.end(); ++iter)
	{	
		++ctr;
		(*iter)->setPriority(ctr);
	}
}
void ToDoList::viewTask(Task& current)
{
	char command = ' ';
	char removechoice = ' ';

	while (toupper(command) != 'M')
	{
		current.display();

		cout << "Viewing a Task" << endl << endl;
		
		cout << "E: Edit Task" << endl;
		cout << "D: Delete Task" << endl;
		cout << "T: View Tasks Tags" << endl;
		cout << "V: View ToDoList" << endl;
		cout << "M: Notebook Browse Menu" << endl << endl;
		
		cout << "Please enter your selection: ";
		cin >> command;

		switch (toupper(command))
		{
		case 'E':
			system("cls");
			current.edit();	
			orderList();
			break;
		
		case 'D':
			cout << "Are you sure?(Y or N):";
			cin >> removechoice;
			if (toupper(removechoice) == 'Y')
			{
				//TODO:test to see if note was deleted
				deleteTask(current);
				command = 'M';
			}
			break;
		
		case 'T':
			cout << "this Tasks tags:" << endl;
			current.displayTags();
			cin.ignore();
			cin.get();
			break;
		
		case 'V':
			display();
			command = 'M';
			break;
		
		case 'M':
			break;

		default:
			cout << "Invalid selection." << endl << endl;
			break;
		}
		system("cls");
	}
}
bool ToDoList::tagTask(Tag &currentTag)
{
	bool tagged;
	Task* currentTask;
	currentTask = addingTagDisplay();
	if (currentTask != NULL)
	{
		currentTask->tag(currentTag);
		tagged = true;
	}
	else 
		tagged = false;
	return tagged;
}
bool ToDoList::tagTask(Tag &currentTag ,const std::string& entryTitle)
{
	bool tagged;
	vector<Task*>::iterator iter;
	Task* currentTask=NULL;

	for(iter = tasks.begin(); iter != tasks.end(); ++iter)
	{	
		if ( (*iter)->getTitle() == entryTitle)
		{
		currentTask = (*iter);
		}
	}
	if (currentTask != NULL)
	{
		currentTask->tag(currentTag);
		tagged = true;
	}
	else 
		tagged = false;
	return tagged;
}

Task* ToDoList::addingTagDisplay()
{
	int ctr=0;
	int choice=0;
	Task* tagged=NULL;
	vector<Task*>::iterator iter;

	system("cls");
	
	cout << "Your ToDoList" << endl << endl;
	//
	//display the to do list
	//

	if (  tasks.empty() )
		cout << "there are no Tasks" << endl;
	else
	{
		for(iter = tasks.begin(), ctr=0; iter != tasks.end(); ++iter)
		{	
			++ctr;
			cout << "[" << ctr << "] ";
			(*iter)->displayTitle();
			cout <<endl;
		}

		cout << "Which note would you like to view (enter number 1 - " << ctr << ")? ";
		cin >> choice;
		// range checked here- if it's out of bounds put choice in bounds
		if (choice < 1)
			choice = 1;
		if (choice > ctr)
			choice = ctr;
				
		--choice;//decriment choice so that it works with vector
				
		system("cls");
				
		tagged= tasks.at(choice);
	}
	return tagged;
}
void ToDoList::deleteTask(Task& t)
{
	bool found = false;

	vector<Task*>::iterator iter = tasks.begin();
	while (!found && iter != tasks.end())
	{
		if ((**iter) == t)
		{
		found = true;
		}
		else
		{
		++iter;
		}
	}
	if (found ==true)
	{
		delete (*iter);
		tasks.erase(iter);
	}
}